﻿#include "draw_setting_dialog.h"
#include "ui_draw_setting_dialog.h"


DrawSettingDialog::DrawSettingDialog(QWidget* parent) :
	QDialog(parent),
	ui(new Ui::DrawSettingDialog)
{
	ui->setupUi(this);

	//隐藏标题文件配色
	ui->label_3->setVisible(false);
	ui->color_3->setVisible(false);


	connect(ui->pushButton_add_, &QPushButton::clicked, this, &DrawSettingDialog::AddNewColorItem);
	connect(ui->pushButton_delete_, &QPushButton::clicked, this, &DrawSettingDialog::DeleteColorItem);

	connect(ui->color_1, &QPushButton::clicked, this, &DrawSettingDialog::SettingOtherColor);
	connect(ui->color_2, &QPushButton::clicked, this, &DrawSettingDialog::SettingOtherColor);
	connect(ui->color_3, &QPushButton::clicked, this, &DrawSettingDialog::SettingOtherColor);
	connect(ui->color_4, &QPushButton::clicked, this, &DrawSettingDialog::SettingOtherColor);
	connect(ui->color_5, &QPushButton::clicked, this, &DrawSettingDialog::SettingOtherColor);
	connect(ui->color_6, &QPushButton::clicked, this, &DrawSettingDialog::SettingOtherColor);
	connect(ui->color_7, &QPushButton::clicked, this, &DrawSettingDialog::SettingOtherColor);
	connect(ui->color_8, &QPushButton::clicked, this, &DrawSettingDialog::SettingOtherColor);

	connect(ui->pushButton_ok_, &QPushButton::clicked, this, &DrawSettingDialog::OK);
	connect(ui->pushButton_cancel_, &QPushButton::clicked, this, &DrawSettingDialog::Cancel);
	connect(ui->pushButton_apply_, &QPushButton::clicked, this, &DrawSettingDialog::Apply);

}

DrawSettingDialog::~DrawSettingDialog()
{
	delete ui;
}

void DrawSettingDialog::InitSettingConfig(const PlotSettingConfig& setting)
{
	plot_config_ = setting;
	//默认曲线颜色初始化
	QVBoxLayout* layout = qobject_cast<QVBoxLayout*>(ui->scrollAreaWidgetContents->layout());
	int layout_count = layout->count();
	if (layout_count > 0)
	{
		//有弹簧
		delete layout->takeAt(layout_count - 1);//删除弹簧
	}
	foreach(auto & plot_color, setting.plots_color)
	{
		QString text = QString(QStringLiteral("第%1曲线")).arg(layout->count() + 1);
		PlotColorSettingItem* item = new PlotColorSettingItem(text, plot_color);
		layout->addWidget(item);
	}
	layout->addStretch();
	ui->scrollArea->verticalScrollBar()->setMaximum(ui->scrollArea->widget()->height());
	ui->scrollArea->verticalScrollBar()->setValue(ui->scrollArea->verticalScrollBar()->maximum());

	//其他颜色配置
	OtherDrawColor(ui->color_1, QString(setting.color_background));
	OtherDrawColor(ui->color_2, QString(setting.color_grid_line));
	OtherDrawColor(ui->color_3, QString(setting.color_title_font));
	OtherDrawColor(ui->color_4, QString(setting.color_axis));
	OtherDrawColor(ui->color_5, QString(setting.color_axis_font));
	OtherDrawColor(ui->color_6, QString(setting.color_assist_line));
	OtherDrawColor(ui->color_7, QString(setting.color_assist_background));
	OtherDrawColor(ui->color_8, QString(setting.color_mark_background));

	//其他属性配置
	ui->spinBox_point_size_->setValue(setting.point_size);
	ui->spinBox_line_width_->setValue(setting.line_width);
	ui->checkBox_cursor_guide_->setChecked(setting.cursor_guide_enable);
}

void DrawSettingDialog::AddNewColorItem()
{
	QVBoxLayout* layout = qobject_cast<QVBoxLayout*>(ui->scrollAreaWidgetContents->layout());
	int layout_count = layout->count();
	if (layout_count > 0)
	{
		//有弹簧
		delete layout->takeAt(layout_count - 1);//删除弹簧
	}
	QString text = QString(QStringLiteral("第%1曲线")).arg(layout_count);
	PlotColorSettingItem* item = new PlotColorSettingItem(text);
	layout->addWidget(item);
	layout->addStretch();
	ui->scrollArea->verticalScrollBar()->setMaximum(ui->scrollArea->widget()->height());
	ui->scrollArea->verticalScrollBar()->setValue(ui->scrollArea->verticalScrollBar()->maximum());
}

void DrawSettingDialog::DeleteColorItem()
{
	QVBoxLayout* layout = qobject_cast<QVBoxLayout*>(ui->scrollAreaWidgetContents->layout());
	int layout_count = layout->count();
	if (layout_count <= 1) return;
	QLayoutItem* child;
	if (layout_count > 1)
	{
		if ((child = layout->takeAt(layout_count - 2)) != 0)
		{
			delete child->widget();
		}
	}
}

void DrawSettingDialog::SettingOtherColor()
{
	QPushButton* btn = qobject_cast<QPushButton*>(sender());
	QColor new_color = QColorDialog::getColor(Qt::white, this, QStringLiteral("颜色选择"));
	if (new_color.isValid())
	{
		QPixmap pix(btn->size());
		pix.fill(new_color);
		btn->setIconSize(btn->size());
		btn->setIcon(QIcon(pix));
	}
	QString col = QString::number(new_color.rgb(), 16);
	btn_color_map_.insert(btn, col);
}

void DrawSettingDialog::GetPlotsColor()
{
	plot_config_.plots_color.clear();
	QVBoxLayout* layout = qobject_cast<QVBoxLayout*>(ui->scrollAreaWidgetContents->layout());
	int layout_count = layout->count();
	for (int i = 0; i < layout_count - 1; ++i)//排除弹簧
	{
		PlotColorSettingItem* item = qobject_cast<PlotColorSettingItem*>(layout->itemAt(i)->widget());
		if (item)
		{
			plot_config_.plots_color.append(item->GetItemColor());
		}
	}
}

void DrawSettingDialog::GetOtherDrawColor()
{
	QMapIterator<QPushButton*, QString> iter(btn_color_map_);
	while (iter.hasNext())
	{
		iter.next();
		QPushButton* btn = iter.key();
		QString col = iter.value();
		if (btn == ui->color_1)
		{
			plot_config_.color_background = col;
		}
		else if (btn == ui->color_2)
		{
			plot_config_.color_grid_line = col;
		}
		else if (btn == ui->color_3)
		{
			plot_config_.color_title_font = col;
		}
		else if (btn == ui->color_4)
		{
			plot_config_.color_axis = col;
		}
		else if (btn == ui->color_5)
		{
			plot_config_.color_axis_font = col;
		}
		else if (btn == ui->color_6)
		{
			plot_config_.color_assist_line = col;
		}
		else if (btn == ui->color_7)
		{
			plot_config_.color_assist_background = col;
		}
		else if (btn == ui->color_8)
		{
			plot_config_.color_mark_background = col;
		}
	}
}

void DrawSettingDialog::GetPlotAttr()
{
	plot_config_.point_size = ui->spinBox_point_size_->value();
	plot_config_.line_width = ui->spinBox_line_width_->value();
	plot_config_.cursor_guide_enable = ui->checkBox_cursor_guide_->isChecked();
}

void DrawSettingDialog::OtherDrawColor(QPushButton* btn, QString color)
{
	QPixmap pix(btn->size());
	QColor col(color.toUInt(NULL, 16));
	pix.fill(col);
	btn->setIconSize(btn->size());
	btn->setIcon(QIcon(pix));
}

void DrawSettingDialog::OK()
{
	GetPlotsColor();
	GetOtherDrawColor();
	GetPlotAttr();
	close();
	emit OkSignal(plot_config_);
}

void DrawSettingDialog::Cancel()
{
	close();
}

void DrawSettingDialog::Apply()
{
	GetPlotsColor();
	GetOtherDrawColor();
	GetPlotAttr();
	emit ApplySignal(plot_config_);
}

PlotColorSettingItem::PlotColorSettingItem(QWidget* parent)
	: QWidget(parent)
	, color_(QString::number(QColor(Qt::white).rgb(), 16))
{
	label = new QLabel(this);
	label->setText(QStringLiteral("第x条曲线"));
	select_btn = new QPushButton(this);
	select_btn->setFixedSize(50, 30);

	QHBoxLayout* hbox = new QHBoxLayout;
	hbox->setContentsMargins(2, 2, 2, 2);
	hbox->setSpacing(5);
	hbox->addWidget(label, 2);
	hbox->addWidget(select_btn, 1);
	hbox->addStretch();
	setLayout(hbox);

	connect(select_btn, &QPushButton::clicked, this, &PlotColorSettingItem::SettingColor);
}

PlotColorSettingItem::PlotColorSettingItem(const QString& text, const QString& color, QWidget* parent)
	: QWidget(parent)
{
	label = new QLabel(text, this);
	select_btn = new QPushButton(this);
	select_btn->setFocusPolicy(Qt::NoFocus);
	select_btn->setFixedSize(50, 30);
	SetItemColor(color);

	QHBoxLayout* hbox = new QHBoxLayout;
	hbox->setContentsMargins(2, 2, 2, 2);
	hbox->setSpacing(5);
	hbox->addWidget(label, 2);
	hbox->addWidget(select_btn, 1);
	hbox->addStretch();
	setLayout(hbox);
	connect(select_btn, &QPushButton::clicked, this, &PlotColorSettingItem::SettingColor);
}

void PlotColorSettingItem::SetItemLabel(const QString& text)
{
	label->setText(text);
}

QString PlotColorSettingItem::GetItemLabel()
{
	return label->text();
}

void PlotColorSettingItem::SetItemColor(const QString& color)
{
	color_ = color;
	QPixmap pix(select_btn->size());
	QColor col(color.toUInt(NULL, 16));
	pix.fill(col);
	select_btn->setIconSize(select_btn->size());
	select_btn->setIcon(QIcon(pix));
}

QString PlotColorSettingItem::GetItemColor()
{
	return color_;
}

void PlotColorSettingItem::SettingColor()
{
	QColor old_color = QColor(GetItemColor().toUInt(NULL, 16));
	QColor new_color = QColorDialog::getColor(old_color, this, QStringLiteral("颜色选择"));
	if (new_color.isValid())
	{
		SetItemColor(QString::number(new_color.rgb(), 16));
	}
}

